package com.feicheng.house.service.impl;

import com.feicheng.house.entity.Record;
import com.feicheng.house.entity.User;
import com.feicheng.house.entity.UserRole;
import com.feicheng.house.exception.SystemException;
import com.feicheng.house.mapper.RecordMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IRecordService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements IRecordService {
	/** 押金记录类别头 */
    private static final String ORDER_CODE = "RE";

	@Autowired
	HttpSession session;

	@Override
	public ResultInfo getRecordInfo(String depositNumber) {
		
		return ResultInfo.success(this.getById(depositNumber));
	}
	
	@Override
	public ResultInfo addRecordInfo(Record record) {
//		JSONObject current = (JSONObject) session.getAttribute("CURRENT");
//		if (current != null) {
//			User currentUser = (User) current.get("user");
//			record.setUserId(currentUser.getUserId());
//		}
        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");//生成时间戳
        record.setDepositNumber(ORDER_CODE + sdf.format(new Date()));
//		record.setUserId(1);
		if (null == record.getDepositId()) {
			throw new SystemException("请输入押金编号！");
		};
		if (null == record.getDepositNumber()) {
			throw new SystemException("请输入押金记录单号！");
		};
		if (null == record.getClientId()) {
			throw new SystemException("请输入客户编号！");
		};
		if (null == record.getUserId()) {
			throw new SystemException("请输入用户编号！");
		};		
		if (null == record.getReason()) {
			throw new SystemException("请输入原因");
		};
		return ResultInfo.success(this.save(record));
	}

	@Override
	public ResultInfo editRecordInfo(Record record) {
		if(null == record.getDepositNumber()) {
			throw new SystemException("订单单号为空");	
		}
		return ResultInfo.success(this.updateById(record));
	}
			
	@Override
	public ResultInfo pagesInfo(Integer pageSize , Integer currentPage , String searchKey) {
		QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<Record>();
		recordQueryWrapper.eq("delete_flag", true);
		if (StringUtils.hasLength(searchKey)) {
			recordQueryWrapper.like("deposit_id", searchKey)
			.or().like("user_id", searchKey)
			.or().like("client_id", searchKey);
		}
		IPage<Record> pageInfo = new Page<Record>(currentPage , pageSize);
		return ResultInfo.success(this.page(pageInfo,recordQueryWrapper));
	}

	
}
